/**
 * Clicky
 *
 * Make a link of selected subjects by the rel attribute.
 * Can set a class or execute given functions on hover.
 * Also sets the pointer as cursor (optionally).
 *
 * @param		attr		String			The attribute name to use as URL.
 * @param		over		String|Function	Class to add or function to run on hover
 * @param		out			Function		Function to run on mouseout (hover) if provided.
 * @param		hand		Boolean			Add curser:pointer css property on true
 * @param		external	Boolean			Open links in a new tab/window on true
 * @subpackage	jQuery
 * @author		Koos van Egmond | 7U Digital Handmade Originals
 */

(function($)
{
	$.fn.clicky = function(opts)
	{
		// Set default options
		var def = {
			attr: 'rel',
			over: 'hover',
			out: false,
			hand: true,
			external: false
		};
		
		// Merge the options
		opts = $.extend(def, opts);
		
		// Set scope for the old status
		var _os;
		
		// Loop through all objects
		return this.each( function()
		{
			// Set this aliases
			var t = $(this);
			var o = opts;
			var u = t.attr(o.attr);
			
			// Set hand cursor if requested
			if (o.hand) {
				t.css( 'cursor', 'pointer' );
			}
			
			// Set click function
			t.click(function()
			{
				// If external, open in new window / tab
				if (o.external) {
					window.open(u, '_blank');
				} else {
					window.location.href = u;
				}
			});
			
			// Set hover function
			t.hover(
				function()
				{
					// Set class or run function?
					if ( typeof o.over == 'function' ) {
						o.over(t);
					} else {
						t.addClass( o.over );
					}
					
					// Set correct statusses
					_os = window.status;
					window.status = u; 
				},
				function()
				{
					// Remove hover class?
					if ( typeof o.over == 'string' ) {
						t.removeClass( o.over );
					}
					
					// Run function ?
					if ( typeof o.out == 'function' ) {
						o.out(t);
					}
				
					// Set correct statusses
					window.status = _os; 
				}
			);
		});
	};
})(jQuery);

